﻿using AspectSharp;
using Mbs.ComponentModel.Container;
using Mbs.ComponentModel.Container.Listner;

namespace Mbs.AOP
{
    public class AOPKernelListner:KernelListnerAdapter
    {
        private IAspectEngine AspectEngine;

        public AOPKernelListner(IAspectEngine aspectEngine)
            : base(LifestyleStage.PreCreate)
        {
            AspectEngine = aspectEngine;
        }

        protected override void Init()
        {
            if (AspectEngine != null)
                Kernel.RegisterInstance("proxyFactory", typeof(Mbs.ComponentModel.Container.Proxy.IProxyFactory), new Mbs.AOP.ProxyFactory(AspectEngine));
        }

        public override void OnPreCreate(Mbs.ComponentModel.Container.Metadata.IComponentInfo info)
        {
            if (AspectEngine != null && AspectEngine.HasMatch(info.Implementation))
                info.ExtendedProperties.Set("proxy", true);

            base.OnPreCreate(info);
        }
    }
}
